<html>
<head><meta charset="utf-8"><title>block_on in std · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html">block_on in std</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="178262402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178262402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178262402">(Oct 16 2019 at 06:24)</a>:</h4>
<p>We talked earlier about having block_on as a minimal executor available in std. It's pretty helpful for experimenting, bridging sync and async code, and does not seem like something that has a very controversial API. The implementation is also very minimal, and probably as efficient as it gets. The Waker is just something that calls <code>thread::unpark</code>: <a href="https://github.com/rust-lang-nursery/futures-rs/blob/master/futures-executor/src/local_pool.rs#L41-L74" target="_blank" title="https://github.com/rust-lang-nursery/futures-rs/blob/master/futures-executor/src/local_pool.rs#L41-L74">https://github.com/rust-lang-nursery/futures-rs/blob/master/futures-executor/src/local_pool.rs#L41-L74</a></p>
<p>What needs to be done to get it into std? Does it need an RFC?</p>



<a name="178267382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178267382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178267382">(Oct 16 2019 at 08:05)</a>:</h4>
<p><span class="user-mention" data-user-id="204219">@Matthias247</span> maybe just make a PR at first to see how the implementation would look like and how much impl complexity it adds?</p>



<a name="178267436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178267436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178267436">(Oct 16 2019 at 08:06)</a>:</h4>
<p>I support the idea that experimenting with async is pretty annoying atm since it requires so much setup</p>



<a name="178267875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178267875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178267875">(Oct 16 2019 at 08:14)</a>:</h4>
<p>I wonder if that <code>static</code> will be optimized out if unused</p>



<a name="178267886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178267886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178267886">(Oct 16 2019 at 08:14)</a>:</h4>
<p>(some people may dislike the cost otherwise)</p>



<a name="178269889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178269889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> stko <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178269889">(Oct 16 2019 at 08:50)</a>:</h4>
<p>The CURRENT_THREAD_NOTIFY static? It seems to be unnecessary. The ENTERED one seems harder to avoid.</p>



<a name="178277247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178277247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178277247">(Oct 16 2019 at 10:47)</a>:</h4>
<p>I have been using tokio’s current_thread_executor a lot even in my actual code where I know I don’t want/need to deal with multiple threads.</p>



<a name="178277438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178277438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178277438">(Oct 16 2019 at 10:50)</a>:</h4>
<p>That one will probably be more complicated, but I would probably just use whatever alternative single_thread runtime if it existed in std.</p>



<a name="178277673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178277673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178277673">(Oct 16 2019 at 10:54)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> you'd probably know if unused statics are removed from the final binary? we have <code>#[used]</code> precisely because they are stripped if unused, right?</p>



<a name="178277770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178277770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178277770">(Oct 16 2019 at 10:56)</a>:</h4>
<p>Unused statics will get removed from the binary (removal is considered by both LLVM and linker independently), yes, but it is very easy for a lazy_static to become considered "used", even if it is overall not.</p>



<a name="178277796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178277796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178277796">(Oct 16 2019 at 10:57)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> right, but lazy_static is presumably not going to be used in libstd</p>



<a name="178277853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178277853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178277853">(Oct 16 2019 at 10:58)</a>:</h4>
<p>Another caveat is that we might end up accidentally using this runtime to implement certain stdlib functions which otherwise look blocking</p>



<a name="178347086"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178347086" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178347086">(Oct 17 2019 at 03:23)</a>:</h4>
<p>enter() is not necessarily needed - it just provides another error message if people would use block_on in recursive fashion. Not sure whether that is required - afaik rust also doesn't protect against other recursion issues.</p>
<p><code>CURRENT_THREAD_NOTIFY</code> is also not necessarily required. We can either always create an <code>Arc&lt;Thread&gt;</code>, or potentially create that lazily when the <code>Waker</code> gets cloned. Both are just more overhead in case <code>block_on</code> is used more than once.</p>



<a name="178347153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/178347153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#178347153">(Oct 17 2019 at 03:25)</a>:</h4>
<p>would this live in <code>task</code> or <code>thread</code>? I actually like <code>std::thread::block_on</code> (or <code>block_on_task</code>/<code>block_on_future</code>/etc) - since it's the thread which gets blocked.</p>



<a name="179161025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/179161025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#179161025">(Oct 27 2019 at 08:20)</a>:</h4>
<p>I started working on this. I think I have found a way on how to make this work  pretty efficient without the thread-local variable. 2 tricks here:<br>
- There is actually no need to get the <code>Thread</code> handle until someone clones the Waker. For many primitive async/await examples that isn't necessary, and I can just check a local boolean flag for waking up.<br>
- Thread in the stdlib is already an <code>Arc&lt;ThreadInner&gt;</code> - which I can exploited</p>



<a name="179161539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/179161539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#179161539">(Oct 27 2019 at 08:37)</a>:</h4>
<p>Hmm, when using async/await in unit-tests for libstd (in the source files directly) I get:</p>
<div class="codehilite"><pre><span></span>error[E0433]: failed to resolve: could not find `poll_with_tls_context` in `future`

error[E0433]: failed to resolve: could not find `from_generator` in `future`
</pre></div>


<p>I guess at that compilation step some part for the generators is missing? Is there any way around it, e.g. by placing the test somewhere else? <span class="user-mention" data-user-id="126931">@centril</span>?</p>



<a name="179163320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/179163320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#179163320">(Oct 27 2019 at 09:31)</a>:</h4>
<p>nevermind. Got it running by moving the test into a file in the tests/ subfolder</p>



<a name="179182104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/block_on%20in%20std/near/179182104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/block_on.20in.20std.html#179182104">(Oct 27 2019 at 18:49)</a>:</h4>
<p>PR: <a href="https://github.com/rust-lang/rust/pull/65875" target="_blank" title="https://github.com/rust-lang/rust/pull/65875">https://github.com/rust-lang/rust/pull/65875</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>